package weekMatch.one_343;

public class LeeCode_04 {
    public static void main(String[] args) {
//        System.out.println(smallestBeautifulString("abcz", 26));
        System.out.println(smallestBeautifulString("dc", 4));
    }
    private static String smallestBeautifulString(String s, int k) {
        char[] cs = s.toCharArray();
        char last = (char) ('a' + k);
        int n = s.length(), i = n - 1;
        cs[i]++;
        while (i < n){
            if (cs[i] == last){
                if (i == 0)
                    return "";
                cs[i] = 'a';
                ++cs[--i];
            }else {
                if ((i > 0 && cs[i] == cs[i - 1]) || (i > 1 && cs[i] == cs[i - 2])){
                    ++cs[i];
                }else {
                    i++;
                }
            }
        }
        return new String(cs);
    }
}
